//
//  Reg1ViewController.m
//  KIASupervisory
//
//  Created by apple on 15/7/20.
//  Copyright (c) 2015年 MyCompany. All rights reserved.
//

#import "Reg1ViewController.h"
#import "Reg2ViewController.h"
#import "LoginViewController.h"
#import "CommonPickerViewController.h"
@interface Reg1ViewController ()<CommonPickerDelegate>

@property (nonatomic, strong) NSArray *_cityList;
@property (nonatomic, strong) UIButton *back;
@property (nonatomic, strong) CommonPickerViewController *picker;
@property (nonatomic, strong) NetworkManager *networkManager;
@property (weak, nonatomic) IBOutlet UILabel *cityLbl;
@property (weak, nonatomic) IBOutlet UILabel *cityIdLbl;
@property (weak, nonatomic) IBOutlet UILabel *xiaoquLbl;
@property (weak, nonatomic) IBOutlet UILabel *xiaoquIdLbl;
@property (nonatomic, strong) NSMutableArray *componentData;
@property (nonatomic, strong) NSMutableArray *cityIdData;
@property (nonatomic, strong) NSMutableArray *xiaoquData;
@property (nonatomic, strong) NSMutableArray *xiaoquIdData;
@property (nonatomic, strong) NSString *city_id;
@property (nonatomic, strong) NSString *xiaoqu_id;
@property (nonatomic, strong) MBProgressHUD *HUD;

@end

@implementation Reg1ViewController


- (void)viewDidLoad
{
    [self.navigationController setNavigationBarHidden:YES];
    [super viewDidLoad];
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.HUD];
    self.HUD.delegate = self;
}

- (IBAction)backToPre:(id)sender {
    LoginViewController *problemVC = (LoginViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:problemVC];
    DDMenuController *menuVC = [[DDMenuController alloc] initWithRootViewController:navController];
//    [self presentViewController:menuVC animated:YES completion:^{}];
//    [self.navigationController pushViewController:menuVC animated:YES];
    CATransition* transition = [CATransition animation];
    transition.duration = 0.5;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
    transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    [self.navigationController pushViewController:menuVC animated:NO];
}

- (IBAction)getCityList:(id)sender {
    NSLog(@"@@@@@@@@@@@@@@@@getCityList");
 
    if (self.networkManager == nil) {
        self.networkManager = [[NetworkManager alloc] init];
    }
        [self.networkManager requestWithMethod:getCityList parameter:nil delegate:self];
        [self.HUD show:YES];
}

- (IBAction)getXiaoquList:(id)sender {
    NSLog(@"@@@@@@@@@@@@@@@@getXiaoquList");
    
    if (self.networkManager == nil) {
        self.networkManager = [[NetworkManager alloc] init];
    }
    if(_componentData == nil){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:@"请选择城市"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
       
    } else {
        NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                               _city_id,@"city_id", nil];
        [self.HUD show:YES];
        [self.networkManager requestWithMethod:getXiaoquList parameter:param delegate:self];
    }
}

- (IBAction)next:(id)sender {
    NSLog(@"@@@@@@@@@@@@@@@@getXiaoquList");
    
    if (self.networkManager == nil) {
        self.networkManager = [[NetworkManager alloc] init];
    }
    if(_xiaoqu_id == nil){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:@"请选择小区"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        
    } else if(_city_id == nil){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:@"请选择城市"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        
    } else {
        Reg2ViewController *reportVC = (Reg2ViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"Reg2ViewController"];
        reportVC.xiaoqu_id = _xiaoqu_id;
        [self.navigationController pushViewController:reportVC animated:YES];
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    //    self.navigationController.navigationBarHidden = NO;
    
    //[self performSelector:@selector(getCityList) withObject:nil afterDelay:0.3];
}

- (void)network:(NetworkManager *)net methodId:(RequestMethodId)methodId didRequestFinishedWithResult:(id)data {
    [self.HUD hide:YES];
    //[self endRefresh];
    //self.isRefreshing = NO;
  
    NSLog(@"@@@@@@@@@@@@@@@@111111111getCityList");
    if (methodId == getCityList) {
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSString * result = [data objectForKeyOrNil:@"result"];
            if([result isEqualToString:@"000"]){
                self.picker = (CommonPickerViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"CommonPicker"];
                _picker.delegate = self;
                NSArray *componentData2 = data[@"cityList"];
                _componentData = [[NSMutableArray alloc] init];
                _cityIdData = [[NSMutableArray alloc] init];
                for(id obj in componentData2){
                    NSString *unicodeStr = [obj[@"city"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                    [_componentData addObject:unicodeStr];
                    [_cityIdData addObject:obj[@"id"]];
                }
               
                _picker.datas = [NSArray arrayWithObjects:_componentData, nil];
                _picker.view.tag = 20005;
                _picker.view.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.bounds));
                AppDelegate *app = [AppDelegate sharedDelegate];
                [app.window addSubview:_picker.view];
            }
            
            
        }

    } else if (methodId == getXiaoquList) {
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSString * result = [data objectForKeyOrNil:@"result"];
            if([result isEqualToString:@"000"]){
                self.picker = (CommonPickerViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"CommonPicker"];
                _picker.delegate = self;
                NSArray *componentData2 = data[@"xiaoquList"];
                _xiaoquData = [[NSMutableArray alloc] init];
                _xiaoquIdData = [[NSMutableArray alloc] init];
                for(id obj in componentData2){
                    NSString *unicodeStr = [obj[@"name"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                    [_xiaoquData addObject:unicodeStr];
                    [_xiaoquIdData addObject:obj[@"id"]];
                }
                
                _picker.datas = [NSArray arrayWithObjects:_xiaoquData, nil];
                _picker.view.tag = 20006;
                _picker.view.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.bounds));
                AppDelegate *app = [AppDelegate sharedDelegate];
                [app.window addSubview:_picker.view];
            }
        }
    }
}

- (void)network:(NetworkManager *)net methodId:(RequestMethodId)methodId didRequestFailedWithError:(NSError *)error {
    //[self.HUD hide:YES];
    //[self endRefresh];
    //self.isRefreshing = NO;
    
    NSDictionary *errorInfo = error.userInfo;
    NSString *msg = [errorInfo objectForKeyOrNil:@"errorMsg"];
    
    NSInteger code = error.code;
    if (code != 1) {
        if ([msg length] == 0) {
            msg = @"历史未完成问题获取失败，请稍后重试";
        }
    }
    
    if ([msg length] > 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:msg
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
    }
}


#pragma mark - CommonPickerDelegate
- (void)picker:(CommonPickerViewController *)picker didSelectedItemIndexSet:(NSDictionary *)selectedItems {
    NSInteger index = [[selectedItems objectForKey:[NSNumber numberWithInt:0]] integerValue];
    
    NSInteger tag = picker.view.tag;
     if (tag == 20005) {
        self.cityLbl.text = [_componentData objectAtIndex:index];
         NSLog(@"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@11111@%@",[_cityIdData objectAtIndex:index]);
         _city_id  = [_cityIdData objectAtIndex:index];
                  NSLog(@"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2222222");
//        NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
//                                idStr,@"city_id", nil];
//        [self.networkManager requestWithMethod:getXiaoquList parameter:param delegate:self];
     } else if (tag == 20006){
         self.xiaoquLbl.text = [_xiaoquData objectAtIndex:index];
         //self.xiaoquIdLbl.text = [_xiaoquIdData objectAtIndex:index];
         _xiaoqu_id = [_xiaoquIdData objectAtIndex:index];
     }
}


@end

